import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@WebServlet(name = "FileDownServlet",urlPatterns = "/downfile")
public class FileDownServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/x-msdownload");
        resp.setHeader("Content-Disposition", "attachment;filename=" + new String("测试文档123".getBytes(), "ISO-8859-1"));
        ServletContext context=this.getServletContext();
        String path=context.getRealPath("WEB-INF/it.properties");
        InputStream in =new FileInputStream(path);
        OutputStream out =resp.getOutputStream();
                int b=0;
                while (true){
            b=in.read();
            out.write(b);
            if(b==-1){
                break;
            }
            System.out.println(b);
        }
            in.close();
                out.close();
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);

    }
}
